﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace BusinessLogic
{
    public class ImportCarAdvanceCommand : BaseCommand<object>
    {
        DataTable _list;

        public ImportCarAdvanceCommand(DataTable s)
        {
            _list = s;

            SetQuery("EXEC IMPORT_CAR_ADV @vin,@carseries,@model,@suffix,@saledate");
        }

        public override void Command()
        {
            int succ = 0;
            SqlDataReader dr;

            this.cmd.Parameters.Add("vin", System.Data.SqlDbType.VarChar);
            this.cmd.Parameters.Add("carseries", System.Data.SqlDbType.VarChar);
            this.cmd.Parameters.Add("model", System.Data.SqlDbType.VarChar);
            this.cmd.Parameters.Add("suffix", System.Data.SqlDbType.VarChar);
            this.cmd.Parameters.Add("saledate", System.Data.SqlDbType.DateTime);
            

            for (int i = 0; i < _list.Rows.Count; i++ )
            {
                try
                {
                    if (_list.Rows[i][0].ToString() != "" && _list.Rows[i][0].ToString().ToLower() != "vin")
                    {
                        cmd.Parameters["vin"].Value = _list.Rows[i][0].ToString();
                        cmd.Parameters["carseries"].Value = _list.Rows[i][1].ToString();
                        cmd.Parameters["model"].Value = _list.Rows[i][2].ToString();
                        cmd.Parameters["suffix"].Value = _list.Rows[i][3].ToString();
                        cmd.Parameters["saledate"].Value = Convert.ToDateTime(_list.Rows[i][4]); 

                        dr = cmd.ExecuteReader();
                        dr.Read();

                        if (dr.HasRows)
                            if ((int)dr[0] == 1)
                                succ++;
                        
                        dr.Close();
                    }
                }
                catch (Exception)
                {

                }
            }

            Result = succ;
        }
    }
}
